package com.heima.user.config;


import com.heima.common.user.ExcelConstants;
import com.heima.user.service.ApUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;

/**
 * @author huan
 * @serial 每天一百行, 致敬未来的自己
 * @Description
 */
@Component
public class ApUserPhoneInit implements CommandLineRunner {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private ApUserService apUserService;

    @Override
    public void run(String... args) throws Exception {
        // 先清空数据
        Set<String> set = stringRedisTemplate.opsForSet().members(ExcelConstants.PHONE_INIT);
        if (set.size() != 0) {
            stringRedisTemplate.delete(ExcelConstants.PHONE_INIT);
        }
        //查询所有用户手机号
        List<String> phoneList = apUserService.findAllPhone();
        String[] array = phoneList.toArray(new String[phoneList.size()]);
        stringRedisTemplate.opsForSet().add(ExcelConstants.PHONE_INIT, array);
    }
}
